package dk.contix.ant.gwt;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.types.FileSet;

import com.google.gwt.dev.util.log.PrintWriterTreeLogger;

public class CompileConfig {
	private String destdir;
	private String style = "obfuscated";
	private String level;
	private boolean optimize = false;
	private List filesets = new ArrayList();
	private int workers = 1;
	private String genDir;
	private String workDir;

	/**
	 * Set the destination directory.
	 * 
	 * The directory must exist before the task is executed.
	 * 
	 * @param destdir Dir where generated files are placed.
	 */
	public void setDestdir(String destdir) {
		this.destdir = destdir;
	}
	
	/**
	 * Set the compile style.
	 * @param style Style. Available values are: "obfuscated", "pretty", "detailed". Default is obfuscated.
	 */
	public void setStyle(String style) {
		this.style = style;
	}

	/**
	 * Set the log level for the compiler.
	 * @param level "all", "trace", "spam", "debug", "info", "warn", "error". Default is info.
	 */
	public void setLoglevel(String level) {
		this.level = level;
	}
	
	/**
	 * Enable or disable optimization.
	 * 
	 * When this is set to <code>true</code>, the compiler is only run if any of the included java files have been changed since last compilation.
	 * 
	 * @param optimize <code>true</code> to enable, <code>false</code> to disable. By default, optimization is disabled.
	 */
	public void setOptimize(String optimize) {
		this.optimize = Boolean.valueOf(optimize).booleanValue();
	}

	/**
	 * Add a fileset containing the modules to compile.
	 * 
	 * This should be a set of files matching *.gwt.xml.
	 * @param fileset
	 */
	public void addFileset(FileSet fileset) {
		filesets.add(fileset);
	}
	
	public void validate() throws BuildException {
		if (destdir == null) {
			throw new BuildException("destdir not set");
		} else {
			File dir = new File(destdir);
			if (!dir.exists()) {
				throw new BuildException("Destdir " + destdir + " does not exist");
			}
		}
		if (filesets.size() == 0) {
			throw new BuildException("No filesets included");
		}
	}

	public void setLevel(PrintWriterTreeLogger logger) {
		if (level == null || level.equals("info")) {
			logger.setMaxDetail(PrintWriterTreeLogger.INFO);
		} else if (level.equals("all")) {
			logger.setMaxDetail(PrintWriterTreeLogger.ALL);
		} else if (level.equals("debug")) {
			logger.setMaxDetail(PrintWriterTreeLogger.DEBUG);
		} else if (level.equals("error")) {
			logger.setMaxDetail(PrintWriterTreeLogger.ERROR);
		} else if (level.equals("spam")) {
			logger.setMaxDetail(PrintWriterTreeLogger.SPAM);
		} else if (level.equals("trace")) {
			logger.setMaxDetail(PrintWriterTreeLogger.TRACE);
		} else if (level.equals("warn")) {
			logger.setMaxDetail(PrintWriterTreeLogger.WARN);
		} else {
			throw new BuildException("Unknown log level " + level);
		}
	}
	
	public String getLevel() {
		return level;
	}

	public String getStyle() {
		return style;
	}

	public List getFilesets() {
		return filesets;
	}
	
	public String getDestdir() {
		return destdir;
	}
	
	public boolean isOptimize() {
		return optimize;
	}
	
	public String getGenDir() {
		return genDir;
	}
	
	public void setGenDir(String genDir) {
		this.genDir = genDir;
	}

	public String getWorkDir() {
		return workDir;
	}
	
	public void setWorkDir(String workDir) {
		this.workDir = workDir;
	}
	
	
	public int getWorkers() {
		return workers;
	}
	
	public void setWorkers(int workers) {
		this.workers = workers;
	}
}
